<?php
/**
 * @package Joomla
 * @subpackage Jmonoslideshow
 * @copyright (C) 2010 - Matthieu BARBE - www.ccomca.com
 * @license GNU/GPL v2
 * 
 * Jmonoslideshow is a Joomla extension for Monoslideshow.
 *
 * Jmonoslideshow uses :
 * Monoslideshow (http://www.monoslideshow.com/)
 * Plupload (http://www.plupload.com/) by Moxicode Systems
 * json class by Michal Migurski, Matt Knapp, Brett Stimmerman
 * jquery a JavaScript Library (http://jquery.com/)
 * getid3 library (http://getid3.sourceforge.net/)
 * swfobject is an easy-to-use and standards-friendly method to embed Flash content, which utilizes one small JavaScript file (http://code.google.com/p/swfobject/)
 * Chained Selects jQuery Plugin (http://www.appelsiini.net/2010/jquery-chained-selects)
 *
 * Jmonoslideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class JmonoslideshowControllerJmonoslideshowinstall extends JmonoslideshowController
{
	function __construct() {
		parent::__construct();
		$this->registerTask( 'install'  , 'install' );
		$this->registerTask( 'upgrade'  , 'upgrade' );		
	}

	function install() {		
		$db			= &JFactory::getDBO();
		//$dbPref 	= $db->getPrefix();
		$msgSQL 	= $msgFile = $msgError = '';		
		
		// ------------------------------------------
		// MONOSLIDESHOW
		// ------------------------------------------
		$query =' DROP TABLE IF EXISTS '.$db->nameQuote('#__monoslideshow').';';
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		$query =' CREATE TABLE '.$db->nameQuote('#__monoslideshow').' ('."\n";
		$query.=' '.$db->nameQuote('id').' int(11) unsigned NOT NULL auto_increment,'."\n";
		$query.=' '.$db->nameQuote('asset_id').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('catid').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('albumid').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('title').'  varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('description').' varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('link').' varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('type').' tinyint(1) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('extension').' varchar(4) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('author').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('created').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('modified').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('modified_by').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('checked_out').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('checked_out_time').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('published').' tinyint(1) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('ordering').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('params').' text NOT NULL,'."\n";
		$query.=' '.$db->nameQuote('access').' INTEGER UNSIGNED	NOT NULL DEFAULT \'0\','."\n";
		$query.=' PRIMARY KEY  ('.$db->nameQuote('id').'),'."\n";
		$query.=' KEY '.$db->nameQuote('catid').' ('.$db->nameQuote('catid').','.$db->nameQuote('published').')'."\n";
		$query.=') TYPE=MyISAM CHARACTER SET '.$db->nameQuote('utf8').';'."\n";
		$query.=''."\n";
		
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		// ------------------------------------------
		// MONOSLIDESHOW ALBUM
		// ------------------------------------------
		$query =' DROP TABLE IF EXISTS '.$db->nameQuote('#__monoslideshow_album').';';
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		$query =' CREATE TABLE '.$db->nameQuote('#__monoslideshow_album').' ('."\n";
		$query.=' '.$db->nameQuote('id').' int(11) unsigned NOT NULL auto_increment,'."\n";
		$query.=' '.$db->nameQuote('asset_id').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('catid').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('title').'  varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('description').' varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('author').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('created').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('modified').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('modified_by').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('checked_out').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('checked_out_time').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('published').' tinyint(1) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('ordering').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('params').' text NOT NULL,'."\n";
		$query.=' '.$db->nameQuote('access').' INTEGER UNSIGNED	NOT NULL DEFAULT \'0\','."\n";
		$query.=' PRIMARY KEY  ('.$db->nameQuote('id').')'."\n";
		$query.=') TYPE=MyISAM CHARACTER SET '.$db->nameQuote('utf8').';'."\n";
		$query.=''."\n";
		
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		// ------------------------------------------
		// MONOSLIDESHOW CATEGORY
		// ------------------------------------------
		$query =' DROP TABLE IF EXISTS '.$db->nameQuote('#__monoslideshow_category').';';
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		$query =' CREATE TABLE '.$db->nameQuote('#__monoslideshow_category').' ('."\n";
		$query.=' '.$db->nameQuote('id').' int(11) unsigned NOT NULL auto_increment,'."\n";
		$query.=' '.$db->nameQuote('asset_id').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('title').'  varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('description').' varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('author').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('created').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('modified').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('modified_by').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('checked_out').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('checked_out_time').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('published').' tinyint(1) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('ordering').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('params').' text NOT NULL,'."\n";
		$query.=' '.$db->nameQuote('access').' INTEGER UNSIGNED	NOT NULL DEFAULT \'0\','."\n";
		$query.=' PRIMARY KEY  ('.$db->nameQuote('id').')'."\n";
		$query.=') TYPE=MyISAM CHARACTER SET '.$db->nameQuote('utf8').';'."\n";
		$query.=''."\n";
		
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		// ------------------------------------------
		// MONOSLIDESHOW MODELS
		// ------------------------------------------
		$query =' DROP TABLE IF EXISTS '.$db->nameQuote('#__monoslideshow_model').';';
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		$query =' CREATE TABLE '.$db->nameQuote('#__monoslideshow_model').' ('."\n";
		$query.=' '.$db->nameQuote('id').' int(11) unsigned NOT NULL auto_increment,'."\n";
		$query.=' '.$db->nameQuote('asset_id').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('title').'  varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('description').' varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('author').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('created').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('modified').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('modified_by').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('checked_out').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('checked_out_time').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('published').' tinyint(1) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('ordering').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('params').' text NOT NULL,'."\n";
		$query.=' '.$db->nameQuote('access').' INTEGER UNSIGNED	NOT NULL DEFAULT \'0\','."\n";
		$query.=' PRIMARY KEY  ('.$db->nameQuote('id').')'."\n";
		$query.=') TYPE=MyISAM CHARACTER SET '.$db->nameQuote('utf8').';'."\n";
		$query.=''."\n";
		
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		
		// Error
		if ($msgSQL !='') {
			$msgError .= '<br />' . $msgSQL;
		}
		/*if ($msgFile !='') {
			$msgError .= '<br />' . $msgFile;
		}*/
		
		// End Message
		if ($msgError !='') {
			$msg = JText::_( 'COM_JMONOSLIDESHOW_MONOSLIDESHOW_NOT_SUCCESFULLY_INSTALLED' ) . ': ' . $msgError;
		} else {
			$msg = JText::_( 'COM_JMONOSLIDESHOW_MONOSLIDESHOW_SUCCESFULLY_INSTALLED' );
		}
		
		$link = 'index.php?option=com_jmonoslideshow';
		$this->setRedirect($link, $msg);
	}
	
	function upgrade() {
		
		$db			=& JFactory::getDBO();
		//$dbPref 	= $db->getPrefix();
		$msgSQL 	= $msgFile = $msgError = '';
		$user =& JFactory::getUser();
		
		// ------------------------------------------
		// MONOSLIDESHOW ALBUM
		// ------------------------------------------
		
		
		$query =' CREATE TABLE IF NOT EXISTS '.$db->nameQuote('#__monoslideshow_album').' ('."\n";
		$query.=' '.$db->nameQuote('id').' int(11) unsigned NOT NULL auto_increment,'."\n";
		$query.=' '.$db->nameQuote('asset_id').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('catid').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('title').'  varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('description').' varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('author').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('created').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('modified').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('modified_by').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('checked_out').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('checked_out_time').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('published').' tinyint(1) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('ordering').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('params').' text NOT NULL,'."\n";
		$query.=' '.$db->nameQuote('access').' INTEGER UNSIGNED	NOT NULL DEFAULT \'0\','."\n";
		$query.=' PRIMARY KEY  ('.$db->nameQuote('id').')'."\n";
		$query.=') TYPE=MyISAM CHARACTER SET '.$db->nameQuote('utf8').';'."\n";
		
		
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		// ------------------------------------------
		// MONOSLIDESHOW CATEGORY
		// ------------------------------------------
		
		$query =' CREATE TABLE IF NOT EXISTS '.$db->nameQuote('#__monoslideshow_category').' ('."\n";
		$query.=' '.$db->nameQuote('id').' int(11) unsigned NOT NULL auto_increment,'."\n";
		$query.=' '.$db->nameQuote('asset_id').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('title').'  varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('description').' varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('author').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('created').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('modified').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('modified_by').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('checked_out').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('checked_out_time').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('published').' tinyint(1) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('ordering').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('params').' text NOT NULL,'."\n";
		$query.=' '.$db->nameQuote('access').' INTEGER UNSIGNED	NOT NULL DEFAULT \'0\','."\n";
		$query.=' PRIMARY KEY  ('.$db->nameQuote('id').')'."\n";
		$query.=') TYPE=MyISAM CHARACTER SET '.$db->nameQuote('utf8').';'."\n";
		
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		// ------------------------------------------
		// MONOSLIDESHOW MODELS
		// ------------------------------------------
		
		$query =' CREATE TABLE IF NOT EXISTS '.$db->nameQuote('#__monoslideshow_model').' ('."\n";
		$query.=' '.$db->nameQuote('id').' int(11) unsigned NOT NULL auto_increment,'."\n";
		$query.=' '.$db->nameQuote('asset_id').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('title').'  varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('description').' varchar(255) NOT NULL default \'\','."\n";
		$query.=' '.$db->nameQuote('author').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('created').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('modified').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('modified_by').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('checked_out').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('checked_out_time').' datetime NOT NULL default \'0000-00-00 00:00:00\','."\n";
		$query.=' '.$db->nameQuote('published').' tinyint(1) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('ordering').' int(11) NOT NULL default \'0\','."\n";
		$query.=' '.$db->nameQuote('params').' text NOT NULL,'."\n";
		$query.=' '.$db->nameQuote('access').' INTEGER UNSIGNED	NOT NULL DEFAULT \'0\','."\n";
		$query.=' PRIMARY KEY  ('.$db->nameQuote('id').')'."\n";
		$query.=') TYPE=MyISAM CHARACTER SET '.$db->nameQuote('utf8').';'."\n";
		
		$db->setQuery( $query );
		if (!$result = $db->query()){$msgSQL .= $db->stderr() . '<br />';}
		
		// UPGRADE MONOSLIDESHOW  VERSION
	
		$updateAuthor 	= false;
		$errorMsg	= '';
		$updateAuthor = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "author", "INT( 11 ) NOT NULL DEFAULT '0'", "description" );
		if (!$updateAuthor) {
			$msgSQL .= 'Error while updating AUTHOR column';
		}
		
		$updateCreate 	= false;
		$errorMsg	= '';
		$updateCreate = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "created", "datetime NOT NULL default '0000-00-00 00:00:00'", "author" );
		if (!$updateCreate) {
			$msgSQL .= 'Error while updating CREATED column';
		}
		
		$updateLink 	= false;
		$errorMsg	= '';
		$updateLink = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "link", "varchar(255) NOT NULL default ''", "description" );
		if (!$updateLink) {
			$msgSQL .= 'Error while updating LINK column';
		}
		
		$updateModifier 	= false;
		$errorMsg	= '';
		$updateModifier = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "modified", "datetime NOT NULL default '0000-00-00 00:00:00'", "created" );
		if (!$updateModifier) {
			$msgSQL .= 'Error while updating modified column';
		}
		
		$updateModifiedBy 	= false;
		$errorMsg	= '';
		$updateModifiedBy = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "modified_by", "INT( 11 ) NOT NULL DEFAULT '0'", "modified" );
		if (!$updateModifiedBy) {
			$msgSQL .= 'Error while updating modified_by column';
		}
		
		$updateType = false;
		$errorMsg	= '';
		$updateType = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "type", "tinyint( 1 ) NOT NULL DEFAULT '0'", "link" );
		if (!$updateType) {
			$msgSQL .= 'Error while updating type column';
		}
		
		$updateParams = false;
		$errorMsg	= '';
		$updateParams = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "params", "text NOT NULL", "ordering" );
		if (!$updateParams) {
			$msgSQL .= 'Error while updating params column';
		}
		
		$updateExtension = false;
		$errorMsg	= '';
		$updateExtension = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "extension", "varchar(4) NOT NULL default ''", "type" );
		if (!$updateExtension) {
			$msgSQL .= 'Error while updating extension column';
		}
		
		$updateAlbumid = false;
		$errorMsg	= '';
		$updateAlbumid = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "albumid", "int(11) NOT NULL default '0'", "catid" );
		if (!$updateAlbumid) {
			$msgSQL .= 'Error while updating albumid column';
		}
		
		$updateAssetid = false;
		$errorMsg	= '';
		$updateAssetid = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "asset_id", "int(11) NOT NULL default '0'", "id" );
		if (!$updateAssetid) {
			$msgSQL .= 'Error while updating asset_id column';
		}
		
		$updateAssetidCategory = false;
		$errorMsg	= '';
		$updateAssetidCategory = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow_category", "asset_id", "int(11) NOT NULL default '0'", "id" );
		if (!$updateAssetidCategory) {
			$msgSQL .= 'Error while updating asset_id column for category table';
		}

		$updateAssetidModel = false;
		$errorMsg	= '';
		$updateAssetidModel = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow_model", "asset_id", "int(11) NOT NULL default '0'", "id" );
		if (!$updateAssetidModel) {
			$msgSQL .= 'Error while updating asset_id column for category table';
		}
		
		$updateAssetidAlbum = false;
		$errorMsg	= '';
		$updateAssetidAlbum = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow_album", "asset_id", "int(11) NOT NULL default '0'", "id" );
		if (!$updateAssetidAlbum) {
			$msgSQL .= 'Error while updating asset_id column for category table';
		}
		
		$updateAccessMedia = false;
		$errorMsg	= '';
		$updateAccessMedia = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "access", "INTEGER UNSIGNED	NOT NULL DEFAULT '0'", "params" );
		if (!$updateAccessMedia) {
			$msgSQL .= 'Error while updating asset_id column for category table';
		}
		
		$updateAccessCategory = false;
		$errorMsg	= '';
		$updateAccessCategory = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow_category", "access", "INTEGER UNSIGNED	NOT NULL DEFAULT '0'", "params" );
		if (!$updateAccessCategory) {
			$msgSQL .= 'Error while updating asset_id column for category table';
		}
		
		$updateAccessAlbum = false;
		$errorMsg	= '';
		$updateAccessAlbum = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow_album", "access", "INTEGER UNSIGNED	NOT NULL DEFAULT '0'", "params" );
		if (!$updateAccessAlbum) {
			$msgSQL .= 'Error while updating asset_id column for category table';
		}
		
		$updateAccessModel = false;
		$errorMsg	= '';
		$updateAccessModel = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow_model", "access", "INTEGER UNSIGNED	NOT NULL DEFAULT '0'", "params" );
		if (!$updateAccessModel) {
			$msgSQL .= 'Error while updating asset_id column for category table';
		}
		
		//move datas from #__categories to #__monoslideshow_category 
		$query =' SELECT * FROM '.$db->nameQuote('#__categories').' WHERE '.$db->nameQuote('section').' = '.$db->quote('com_jmonoslideshow').'; ';
		$db->setQuery( $query );
		if (!$db->query()) {
			$msgSQL .= $db->getErrorMsg(). '<br />';
		  }

		$num_rows = $db->getNumRows();
		if ($num_rows != 0)
			{
		
				if (!($rows = $db->loadObjectList())) {
					$msgSQL .= $db->getErrorMsg(). '<br />';
				}
		
				foreach ($rows as $row) {
					
					$move = 'INSERT INTO '.$db->nameQuote('#__monoslideshow_category').' (id, title, description, author, created, ordering, published) VALUE ('.$db->quote($row->id).', '.$db->quote($row->title).','.$db->quote($row->description).','.$db->quote($user->get('id')).', '.$db->quote(date('Y-m-d H:i:s')).' , '.$db->quote($row->ordering).', '.$db->quote($row->published).');';
					$db->setQuery( $move );
					if (!$db->query()) {
						$msgSQL .= $db->getErrorMsg(). '<br />';
					}
					
					$delete = 'DELETE FROM '.$db->nameQuote('#__categories').' WHERE '.$db->nameQuote('section').' = '.$db->quote('com_jmonoslideshow').' AND id = '.$db->quote($row->id).';';
					$db->setQuery( $delete );
					if (!$db->query()) {
						$msgSQL .= $db->getErrorMsg(). '<br />';
					}
				}
			}
		
		// CHECK TABLES
		$query =' SELECT * FROM '.$db->nameQuote('#__monoslideshow').' LIMIT 1;';
		$db->setQuery( $query );
		$result = $db->loadResult();
		if ($db->getErrorNum()) {
			$msgSQL .= $db->getErrorMsg(). '<br />';
		}
		
		$query =' SELECT * FROM '.$db->nameQuote('#__monoslideshow_album').' LIMIT 1;';
		$db->setQuery( $query );
		$result = $db->loadResult();
		if ($db->getErrorNum()) {
			$msgSQL .= $db->getErrorMsg(). '<br />';
		}
		
		$query =' SELECT * FROM '.$db->nameQuote('#__monoslideshow_category').' LIMIT 1;';
		$db->setQuery( $query );
		$result = $db->loadResult();
		if ($db->getErrorNum()) {
			$msgSQL .= $db->getErrorMsg(). '<br />';
		}

		// Error
		if ($msgSQL !='') {
			$msgError .= '<br />' . $msgSQL;
		}
		/*if ($msgFile !='') {
			$msgError .= '<br />' . $msgFile;
		}*/	
		// End Message
		
		// DELETE FILES
		jimport('joomla.filesystem.file');
		$admin = JPATH_ROOT.DS . 'administrator' . DS . 'components' . DS . 'com_jmonoslideshow' ;
		if (JFile::exists($admin.DS."views".DS."image".DS."view.raw.php")) { JFile::delete($admin.DS."views".DS."image".DS."view.raw.php");}
		if (JFile::exists($admin.DS."views".DS."album".DS."view.raw.php")) { JFile::delete($admin.DS."views".DS."album".DS."view.raw.php");}
		if ($msgError !='') {
			$msg = JText::_( 'COM_JMONOSLIDESHOW_MONOSLIDESHOW_NOT_SUCCESSFULLY_UPGRADED' ) . ': ' . $msgError;
		} else {
			$msg = JText::_( 'COM_JMONOSLIDESHOW_MONOSLIDESHOW_SUCCESSFULLY_UPGRADED' );
		}
		
		
		$link = 'index.php?option=com_jmonoslideshow';
		
		$this->setRedirect($link, $msg);
	}
	
	
	function AddColumnIfNotExists(&$errorMsg, $table, $column, $attributes = "INT( 11 ) NOT NULL DEFAULT '0'", $after = '' ) {
		
		global $mainframe;
		$db				=& JFactory::getDBO();
		$columnExists 	= false;

		$query = 'SHOW COLUMNS FROM '.$table;
		$db->setQuery( $query );
		if (!$result = $db->query()){return false;}
		$columnData = $db->loadObjectList();
		
		foreach ($columnData as $valueColumn) {
			if ($valueColumn->Field == $column) {
				$columnExists = true;
				break;
			}
		}
		
		if (!$columnExists) {
			if ($after != '') {
				$query = 'ALTER TABLE '.$db->nameQuote($table).' ADD '.$db->nameQuote($column).' '.$attributes.' AFTER '.$db->nameQuote($after).';';
			} else {
				$query = 'ALTER TABLE '.$db->nameQuote($table).' ADD '.$db->nameQuote($column).' '.$attributes.';';
			}
			$db->setQuery( $query );
			if (!$result = $db->query()){return false;}
			$errorMsg = 'notexistcreated';
		}
		
		return true;
	}
}
?>